<ModalDialog
  {id}
  {label}
  {title}
  {className}
  shrinkWidthToFit={true}
  background="var(--main-bg)"
>
  <form class="confirmation-dialog-form">
    <slot></slot>
    <div class="confirmation-dialog-form-flex">
      <button type="button" disabled={confirmationButtonDisabled} on:click="onPositive()">
        {positiveText || 'OK'}
      </button>
      <button type="button" on:click="onNegative()">
        {negativeText || 'Cancel'}
      </button>
    </div>
  </form>
</ModalDialog>
<style>
  .confirmation-dialog-form-flex {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-gap: 10px;
    padding: 10px 20px;
  }
  .confirmation-dialog-form-flex button {
    min-width: 125px;
  }
</style>
<script>
  import ModalDialog from './ModalDialog.html'
  import { show } from '../helpers/showDialog.js'
  import { close } from '../helpers/closeDialog.js'
  import { on } from '../../../_utils/eventBus.js'
  import { oncreate as onCreateDialog } from '../helpers/onCreateDialog.js'

  export default {
    oncreate () {
      on('destroyDialog', this, this.onDestroyDialog)
      onCreateDialog.call(this)
    },
    data: () => ({
      positiveText: undefined,
      negativeText: undefined,
      className: ''
    }),
    methods: {
      show,
      close,
      onDestroyDialog (thisId) {
        const {
          id,
          positiveResult
        } = this.get()
        if (thisId !== id) {
          return
        }
        if (positiveResult) {
          this.fire('positive')
        } else {
          this.fire('negative')
        }
      },
      onPositive () {
        this.set({ positiveResult: true })
        this.close()
      },
      onNegative () {
        this.close()
      }
    },
    components: {
      ModalDialog
    }
  }
</script>
